package com.easyj.admin.sys.serviceimpl;

import java.util.List;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easyj.admin.sys.service.RoleService;
import com.easyj.base.exception.EasyjException;
import com.easyj.base.exception.UpdateSystemObjectException;
import com.easyj.base.exception.ValidateException;
import com.easyj.base.security.service.SecurityUtil;
import com.easyj.admin.db.model.Permission;
import com.easyj.admin.db.mapper.RoleMapper;
import com.easyj.admin.db.mapper.RolePermissionMapper;
import com.easyj.admin.db.mapper.UserRoleMapper;
import com.easyj.admin.db.model.Role;
import com.easyj.admin.db.model.RolePermission;
import com.easyj.admin.db.model.User;




@Service
public class RoleServiceImpl implements RoleService{
	
	@Autowired
	private  RoleMapper roleMapper;
	@Autowired
	private  RolePermissionMapper rolePermissionMapper;
	
	@Autowired
	private UserRoleMapper userRoleMapper;
	
   	public List<Role> findList(Role filter){
        return roleMapper.selectByExample(filter);
    }

	
	public Role findById(Long id){
		Role entity= roleMapper.selectByPrimaryKey(id);
		return entity;
	}
	
	/**
	 * 查找有此角色的用户
	 * @param roleId
	 * @return
	 */
	public  List<User> findUsersByRoleId(Long roleId){
		return userRoleMapper.selectUsersByRoleId(roleId);
	}
	
	/**
	 * 查找有此角色的用户
	 * @param roleId
	 * @return
	 */
	public  List<User> findUsersByRoleCode(String rolecode){
		return userRoleMapper.selectUsersByRoleCode(rolecode);
	}

	@Transactional
	public int  insertEntity(Role entity) throws EasyjException{
		if(roleMapper.selectUniqueByUqCode(entity.getCode())!=null) {
			throw new ValidateException(EasyjException.SameCode,"已有相同编号的角色！");
		}
		if(roleMapper.selectUniqueByUqName(entity.getName())!=null) {
			throw new ValidateException(EasyjException.SameName,"已有相同名称的角色！");
		}
	    entity.setCreateTime(new Date());
	    entity.setIsSystem(false);
	    entity.setOperatorId(SecurityUtil.getOperatorId());
		int rtn= roleMapper.insert(entity);
		return rtn;
	}
	
	@Transactional
	public int  updateEntity(Role entity) throws EasyjException{
		Role old= roleMapper.selectByPrimaryKey(entity.getId());
	    if(old.getIsSystem()) {
	    	throw new UpdateSystemObjectException();
	    }
	    //改为code不可变...
	    //Role find_code=roleMapper.selectUniqueByUqCode(entity.getCode());
	    //if(find_code!=null && find_code.getId().longValue()!=entity.getId().longValue()) {
		//	throw new ValidateException(EasyjException.SameCode,"已有相同编号的角色！");
		//}
	    entity.setCode(old.getCode()); //can not change code
	    
	    Role find_name=roleMapper.selectUniqueByUqName(entity.getName());
		if(find_name!=null && find_name.getId().longValue()!=entity.getId().longValue()) {
			throw new ValidateException(EasyjException.SameName, "已有相同名称的角色！");
		}
	    entity.setModifyTime(new Date()); 
	    entity.setOperatorId(SecurityUtil.getOperatorId());
		int rtn= roleMapper.updateByPrimaryKey(entity);
		return rtn;
	}

	@Transactional
	public int  deleteById(Long id) throws EasyjException{
		 Role entity= roleMapper.selectByPrimaryKey(id);
		 if(entity.getIsSystem()) { //系统角色，不能删除
		    	throw new UpdateSystemObjectException();
		    }
		if( userRoleMapper.countByRoleId(id)>0) {
			throw new ValidateException("角色在使用，不能删除！");
		}
		 //del permission...
		rolePermissionMapper.deleteByRoleId(id);
	    int rtn= roleMapper.deleteByPrimaryKey(id);
		return rtn;
	}
	
	@Transactional
	public int  updatePermision(Long id,Long[] permisionIds) throws EasyjException{
		Role entity=roleMapper.selectByPrimaryKey(id);
		if(entity.getId()==1l) { //
	    	throw new EasyjException(10000,"系统管理员角色(admin)的权限不可修改！");
	    }
		
		entity.setModifyTime(new Date()); 
		entity.setOperatorId(SecurityUtil.getOperatorId());
		int rtn= roleMapper.updateByPrimaryKeySelective(entity);
		
		rolePermissionMapper.deleteByRoleId(entity.getId());
		
		if(permisionIds!=null){
			for(Long permissionId: permisionIds){
				RolePermission r=new RolePermission();
				r.setRoleId(entity.getId());
				r.setPermissionId(permissionId);
				rolePermissionMapper.insert(r);
			}	
		}
		
		return rtn;
	}


	public Role  selectUniqueByUqCode(String code){
	   Role entity= roleMapper.selectUniqueByUqCode(code);
	   return entity;
	}
	
	public Role  selectUniqueByUqName(String name){
	   Role entity= roleMapper.selectUniqueByUqName(name);
	   return entity;
	}
	
	public  List<Permission> findPermissionsByRoleId(Long roleId){
		return rolePermissionMapper.selectByRoleId(roleId);
	}
	
	public  List<Permission> findPermissionsByRoleIds(List<Long> roleIds){
		return rolePermissionMapper.selectByRoleIds(roleIds);
	}
	
}
